﻿using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public enum StepA
        {
            None,
            stepA0,
            stepA1,
            stepA2,
            stepA3,
            stepA4,
            stepA5,
            stepA6,
        }

        public enum StepB
        {
            None,
            stepB0,
            stepB1,
            stepB2,
            stepB3,
            stepB4,
            stepB5,
            stepB6,
        }

        public static bool awaitCondition = false;
        public ManualResetEvent blockFlow = new ManualResetEvent(true);
        StepA stepA = 0;
        StepB stepB = 0;
        CancellationTokenSource cts = new CancellationTokenSource();
        Task taskA, taskB;

        public void RunA()
        {
            while (!cts.IsCancellationRequested)
            {
                switch (stepA)
                {
                    case StepA.stepA0:
                        ALog();
                        stepB = StepB.stepB0;
                        stepA = StepA.stepA1;
                        break;

                    case StepA.stepA1:                     
                        if (awaitCondition)
                        { 
                            stepA = StepA.stepA2;
                            awaitCondition = false;
                            ALog("B已经到位");
                        }
                        else
                        {
                            ALog();
                        }
                        break;

                    case StepA.stepA2:
                        ALog();
                        stepA = StepA.stepA3;
                        break;

                    case StepA.stepA3:
                        ALog();
                        stepA = StepA.stepA0;
                        break;

                    default:                           
                        break;
                }
                blockFlow.WaitOne(-1);
                Thread.Sleep(80);
            }
        }

        public void RunB()
        {
            while (!cts.IsCancellationRequested)
            {
                switch (stepB)
                {
                    case StepB.stepB0:
                        BLog();
                        stepB = StepB.stepB1;
                        break;

                    case StepB.stepB1:
                        BLog();
                        awaitCondition = true;
                        stepB = StepB.stepB2;
                        break;

                    case StepB.stepB2:
                        BLog();
                        stepB = StepB.stepB3;
                        break;

                    case StepB.stepB3:
                        BLog();
                        stepB = StepB.stepB4;
                        break;

                    default:
                        break;
                }
                blockFlow.WaitOne(-1);
                Thread.Sleep(80);
            }
        }

        //启动按钮
        private void Start_Click(object sender, EventArgs e)
        {
            if (stepA == StepA.None)
            { stepA = StepA.stepA0;}
            if (stepB == StepB.None)
            { stepB = StepB.stepB0; }
            blockFlow.Set();
        }

        //暂停按钮
        private void Pasuse_Click(object sender, EventArgs e)
        {
            blockFlow.Reset();
        }

        public void ALog(string mes)
        {
            if (ALogBox == null) return;
            if (!ALogBox.IsHandleCreated) return;
            if (ALogBox.InvokeRequired)
            {
                // 如果需要跨线程访问，使用 Invoke 方法
                ALogBox.Invoke(new Action(() =>
                {
                    ALogBox.AppendText(DateTime.Now.ToString("HH:mm:ss  ")+ mes + Environment.NewLine);
                }));
            }
            else
            {
                // 如果在 UI 线程，直接更新
                ALogBox.AppendText(DateTime.Now.ToString("HH:mm:ss  ") + mes + Environment.NewLine);
            }
        }
        public void ALog()
        {
            if (ALogBox == null) return;
            if (!ALogBox.IsHandleCreated) return;
            if (ALogBox.InvokeRequired)
            {
                // 如果需要跨线程访问，使用 Invoke 方法
                ALogBox.Invoke(new Action(() =>
                {
                    ALogBox.AppendText(DateTime.Now.ToString("HH:mm:ss  ") + stepA.ToString() + Environment.NewLine);
                }));
            }
            else
            {
                // 如果在 UI 线程，直接更新
                ALogBox.AppendText(DateTime.Now.ToString("HH:mm:ss  ") + stepA.ToString() + Environment.NewLine);
            }
        }

        public void BLog()
        {
            if (BLogBox == null) return;
            if (!BLogBox.IsHandleCreated) return;
            if (BLogBox.InvokeRequired)
            {
                // 如果需要跨线程访问，使用 Invoke 方法
                BLogBox.Invoke(new Action(() =>
                {
                    BLogBox.AppendText(DateTime.Now.ToString("HH:mm:ss  ") + stepB.ToString() + Environment.NewLine);
                }));
            }
            else
            {
                // 如果在 UI 线程，直接更新
                BLogBox.AppendText(DateTime.Now.ToString("HH:mm:ss  ") + stepB.ToString() + Environment.NewLine);
            }        
        }
        public void BLog(string mes)
        {
            if (BLogBox == null) return;
            if (!BLogBox.IsHandleCreated) return;
            if (BLogBox.InvokeRequired)
            {
                // 如果需要跨线程访问，使用 Invoke 方法
                BLogBox.Invoke(new Action(() =>
                {
                    BLogBox.AppendText(DateTime.Now.ToString("HH:mm:ss ") + mes + Environment.NewLine);
                }));
            }
            else
            {
                // 如果在 UI 线程，直接更新
                BLogBox.AppendText(DateTime.Now.ToString("HH:mm:ss  ") + mes + Environment.NewLine);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            StartScan();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            StopScan();
        }

        /// <summary>
        /// 开始循环扫描
        /// </summary>
        private void StartScan()
        {
            taskA = Task.Run(RunA);
            taskB = Task.Run(RunB);
        }

        /// <summary>
        /// 停止循环扫描
        /// </summary>
        private void StopScan()
        {
            if (!cts.IsCancellationRequested)
            {
                cts?.Cancel();
            } 
        }       
    }
}
